#include <stdio.h>
#include <string>
#include <iostream>
#include <fstream>
#include <map>

using namespace std;
/*

0. The output will be a text-file, but an intermediate step will be a character array? Or maybe a String Array? (breaking on the spaces)

1. Read in dictionary from a file. This dictionary is a mapping from each of the characters contained in the message to
a corresponding binary string (the mapping is unique to the individual message). This should go into some sort of hash or array?


2. Read in the actual message. Array of C++ style strings? (They will be strings of 0s and 1s)

3. For each of these 'strings', compare it to the keys in the dict. When it matches one, Add that character to the output character array. 
If it doesn;t match any of them, complain!

4. Output the output character array to a file (and to the prompt as well during devel)

*/


void read_in_message() {};

void read_in_huffman_key() {



/* // translator is an associative hash to hold the translations.
// Keys are strings, eg '1001;, and values are chars, eg 'h'.
std::map<std::string, char> translator;   
 

 
// You can assign like this:
translator[100] = 'a';
translator[101] = 'b';

// Now test[2] contains a value.
std::cout << "translator['100'] is: " << translator['100'] << '\n';
std::cout << "translator['101'] is: " << translator['101'] << '\n';
 
// All containers support size()
std::cout << "translator.size() is: " << translator.size();

	
//for each (KeyValuePair<int, String^> kvp in myDictionary) {...}
*/
};

 
map<string, char> Isaacson2() {

    // Here is the declaration of the map
    using namespace std;


	// We will be using a map from strings (the huffman codes for each character ie '1001') to 
	// characters ie 'r'.
    map<string, char> local_map;
    map<string, char>::iterator local_iter;
	
    
    // Here are example strings that have somehow magically appeared in memory (read in from 
	// file.

    string h_code = "000";
    string e_code = "001";
	string l_code = "011";
	string o_code = "010";

	cout << "h_code is: " << h_code << endl;
	cout << "e_code is: " << e_code << endl;
	cout << "l_code is: " << l_code << endl;
	cout << "o_code is: " << o_code << endl;
	
	char h = 'h';
	char e = 'e';
	char l = 'l';
	char o = 'o';
 
	cout << "h is: " << h << endl;
	cout << "e is: " << e << endl;
	cout << "l is: " << l << endl;
	cout << "o is: " << o << endl;
   
    // Here we add the strings to the map

	local_map[h_code] = h;
	local_map[e_code] = e;
	local_map[l_code] = l;
	local_map[o_code] = o;


    // Here is the output of the map in order from beginning to end

    cout << "Here is the dictionary of character codes:" << endl;

    for(local_iter = local_map.begin(); local_iter != local_map.end(); local_iter++)
    {
        cout << (*local_iter).first << " is the code for " << local_iter->second << endl;    
    }
     
   return local_map;
}

int main(int argc, char* argv[]) {

std::string input_line;
char translated_character;
map<string, char> global_map;
map<string, char>::iterator global_iter;

// Read in the dictionary containing a map from huffman codes to characters.
global_map = Isaacson2();

// Here is the dictionary in order from beginning to end
 cout << "Here is the global dictionary of character codes:" << endl;
 for(global_iter = global_map.begin(); global_iter != global_map.end(); global_iter++)
 {
     cout << (*global_iter).first << " is the code for " << global_iter->second << endl;    
 }

// Read in the message. After each huffman code has been read, translate it 
// using the dictionary and write the appropriate character to the output file.

std::ifstream in_file("coded_message.txt");
//std::ofstream out_file("cleartext_message.txt", std::ios::out | std::ios::trunc | std::ios::app );
std::ofstream out_file("cleartext_message.txt");


// Check that we have a message file.
if ( in_file.fail() ){
	 std::cerr << "Can't find a message file to decode!";
	 exit(8);
}
 
while ( !in_file.eof() )
 {
   getline(in_file, input_line);
   translated_character = Isaacson2()[input_line];
   cout << "input_line is " << input_line << " and translated character is '" << translated_character << "'" << endl;
   //cout << flush;
   out_file << translated_character;
   //out_file << flush;
 }

// close both files
in_file.close();
out_file.close();

}

